﻿#region | Copyright © Siriwat Suananpornpanit 2013 |

/*
	##########################################################################
		Software Generator Code For Hashtable From Table Name => TRANSACTIONLOG
		Author By : Siriwat Suananpornpanit
		DateTime : 2013/09/12 11:39:50
	##########################################################################
	*/

#endregion

#region Class | Imports |

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Microsoft.VisualBasic;
using System.Text;
using Web.Dal;
using Web.Prop;

#endregion

namespace Web.Dao
{
    public class TransactionlogDao
    {

        /* for DataBase Connection */

        private static DBConnectionsEngine dbEngine;

        private static System.Data.SqlClient.SqlConnection Conn;

        private static Hashtable hashtable;


        private static TransactionlogDao _dal;

        private System.String sql = "";


        public static TransactionlogDao Init()
        {
            /* for Init */

            dbEngine = new DBConnectionsEngine();

            Conn = new ConnectionsSql().Connection; 

            _dal = new TransactionlogDao();

            return _dal;
        }

        public List<Prop.Transactionlog> Select(Prop.Transactionlog obj)
        {
            List<Prop.Transactionlog> entities = new List<Prop.Transactionlog>();
            try
            {
                System.Text.StringBuilder concast = new System.Text.StringBuilder();

                concast.Append(" ");

                hashtable = new Hashtable();

                if (obj != null)
                {

                    if (!String.IsNullOrEmpty(obj.UID))
                    {
                        concast.Append(" AND UID=:UID ");
                        hashtable.Add(":UID", obj.UID);
                    }

                    if (!String.IsNullOrEmpty(obj.EVENT))
                    {
                        concast.Append(" AND EVENT=:EVENT ");
                        hashtable.Add(":EVENT", obj.EVENT);
                    }
                    /*
                    if (Information.IsDate(obj.UPDATE) )
                    {
                        concast.Append(" AND [UPDATE]=:UPDATE ");
                        hashtable.Add(":UPDATE", obj.UPDATE);
                    }
                    */
                    if (!String.IsNullOrEmpty(obj.LASTIP))
                    {
                        concast.Append(" AND LASTIP=:LASTIP ");
                        hashtable.Add(":LASTIP", obj.LASTIP);
                    }

                    if (!String.IsNullOrEmpty(obj.DESCRIPT))
                    {
                        concast.Append(" AND DESCRIPT=:DESCRIPT ");
                        hashtable.Add(":DESCRIPT", obj.DESCRIPT);
                    }

                }
                sql = System.String.Format(@" SELECT T.ID ,
                                                (
                                                CASE ISNULL((U.FNAME + ' ' + U.LNAME),'')
                                                WHEN '' THEN
                                                'None'
                                                ELSE
                                                (U.FNAME + ' ' + U.LNAME)
                                                END
                                                 )AS [UID]
                                                ,T.EVENT ,T.[UPDATE] ,T.LASTIP ,T.DESCRIPT  FROM TRANSACTIONLOG T
                                                LEFT JOIN USERINFO U ON T.UID = U.UID
                                                ORDER BY T.[UPDATE] DESC");

                sql = sql + concast.ToString();

                entities = dbEngine.ToList<Prop.Transactionlog>(sql, hashtable, Conn).ToList();

            }
            catch (Exception ex)
            {
                entities = null;
            }
            return entities;
        }

        public Boolean Add(Transactionlog model)
        {
            Boolean result = false;
            try
            {
                sql = System.String.Format(@" INSERT INTO TRANSACTIONLOG(UID ,[EVENT] ,[UPDATE] ,LASTIP ,DESCRIPT ) VALUES('{0}' ,'{1}' ,'{2}' ,'{3}' ,'{4}')",
                    model.UID
                     ,
                    model.EVENT
                     ,
                    model.UPDATE
                     ,
                    model.LASTIP
                     ,
                    model.DESCRIPT
                     );

                dbEngine.ExecuteNonQuery(sql, Conn);

            }
            catch (Exception ex)
            {
                result = true;
            }
            return result;
        }

        public Boolean Update(Prop.Transactionlog obj)
        {
            Boolean result = false;
            try
            {
                sql = System.String.Format(@" UPDATE TRANSACTIONLOG
					SET
					UID=:UID,
					EVENT=:EVENT,
					[UPDATE]=:UPDATE,
					LASTIP=:LASTIP,
					DESCRIPT=:DESCRIPT");

                hashtable = new Hashtable();
                hashtable.Add(":UID", obj.UID);
                hashtable.Add(":EVENT", obj.EVENT);
                hashtable.Add(":UPDATE", obj.UPDATE);
                hashtable.Add(":LASTIP", obj.LASTIP);
                hashtable.Add(":DESCRIPT", obj.DESCRIPT);

                dbEngine.ExecuteNonQuery(sql, hashtable, Conn);

            }
            catch (Exception ex)
            {
                result = true;
            }
            return result;
        }

        public Boolean Delete(Prop.Transactionlog obj)
        {
            Boolean result = false;
            try
            {

                System.Text.StringBuilder concast = new System.Text.StringBuilder();

                concast.Append(" WHERE 1=1 ");

                hashtable = new Hashtable();

                if (obj != null)
                {
                    if (!String.IsNullOrEmpty(obj.UID))
                    {
                        concast.Append(" AND UID=:UID ");
                        hashtable.Add(":UID", obj.UID);
                    }

                    if (!String.IsNullOrEmpty(obj.EVENT))
                    {
                        concast.Append(" AND EVENT=:EVENT ");
                        hashtable.Add(":EVENT", obj.EVENT);
                    }

                    if (Information.IsDate(obj.UPDATE))
                    {
                        concast.Append(" AND UPDATE=:UPDATE ");
                        hashtable.Add(":UPDATE", obj.UPDATE);
                    }

                    if (!String.IsNullOrEmpty(obj.LASTIP))
                    {
                        concast.Append(" AND LASTIP=:LASTIP ");
                        hashtable.Add(":LASTIP", obj.LASTIP);
                    }

                    if (!String.IsNullOrEmpty(obj.DESCRIPT))
                    {
                        concast.Append(" AND DESCRIPT=:DESCRIPT ");
                        hashtable.Add(":DESCRIPT", obj.DESCRIPT);
                    }

                }
                sql = System.String.Format(@" DELETE FROM TRANSACTIONLOG ");

                sql = sql + concast.ToString();

                dbEngine.ExecuteNonQuery(sql, hashtable, Conn);

            }
            catch (Exception ex)
            {
                result = true;
            }
            return result;
        }

    }
}

/*
##########################################################################
    Free Software License
##########################################################################
*/

